﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MasMembers.DAL;

namespace MasMembers
{
    public partial class EvaluationForm : Form
    {
        private Int32 currentMem = -1;
        private Int32 currentSW = -1;
        public UserEntity currentUser = null;

        private const String MESSAGE_ERROR_IN_SAVE = "error";
        private const String MESSAGE_SAVED_SUCCESSFULLY = "successs";
        private const String MESSAGE_MEM_NOT_SELECTED = "notExist";
        private const String MESSAGE_DATA_NOT_COMPLETED = "data not completed";
        private const String MESSAGE_DELETE_SUCCESSFULLY = "done";


        public EvaluationForm()
        {
            InitializeComponent();
        }

        private void showMessage(String messageType)
        {
            String message = "";
            MessageBoxIcon icons = MessageBoxIcon.Error;
            switch (messageType)
            {
                case MESSAGE_MEM_NOT_SELECTED: message = "يجب إختيار بيان أولاً"; break;
                case MESSAGE_ERROR_IN_SAVE: message = "خطأ أثناء الحفظ"; break;
                case MESSAGE_DATA_NOT_COMPLETED: message = "يجب إدخال كل البيانات المطلوبة"; break;
                case MESSAGE_DELETE_SUCCESSFULLY: message = "تم الحذف بنجاح"; icons = MessageBoxIcon.Information; break;
                case MESSAGE_SAVED_SUCCESSFULLY: message = "تم الحفظ بنجاح"; icons = MessageBoxIcon.Information; break;
            }
            MessageBox.Show(message, "رسالة", MessageBoxButtons.OK, icons);
        }

        private bool showConfirmDeleteMessage()
        {
            DialogResult result = MessageBox.Show("هل تريد حذف هذا البيان ؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult.Yes == result)
                return true;
            else
                return false;
        }

        private void publishEvalEntityToForm(EvaluationEntity entity)
        {
            txtClosesLevel.Text = entity.e_Closes_Level;
            txtMemId.Text = entity.MemSerialInGroup.ToString();
            pkrJoinDate.Value = DateTime.Parse(entity.e_Date_Of_Join);
        }

        private void publishSWEntityToForm(SocialWorkEntity entity)
        {
            txtSWJobTittle.Text = entity.JobTittle;
            txtSWPlace.Text = entity.Place;
            txtSWSupervisor.Text = entity.Supervisor;
            currentSW = entity.Id;
        }

        private EvaluationEntity fillEvalEntityFromForm()
        {
            EvaluationEntity entity = new EvaluationEntity();
            entity.e_Closes_Level = txtClosesLevel.Text;
            entity.MemSerialInGroup = Int32.Parse(txtMemId.Text);
            entity.e_Date_Of_Join = pkrJoinDate.Value.ToShortDateString();
            entity.e_Modified_By = currentUser.Id;
            entity.Mem_Id = currentMem;
            return entity;

        }

        private SocialWorkEntity fillSWEntity()
        {
            SocialWorkEntity entity = new SocialWorkEntity();
            entity.JobTittle = txtSWJobTittle.Text;
            entity.MemId = currentMem;
            entity.Place = txtSWPlace.Text;
            entity.Supervisor = txtSWSupervisor.Text;
            entity.Id = currentSW;
            return entity;
        }

        private void clearAllFormData()
        {
            txtClosesLevel.Text = "";
            txtMemId.Text = "";
            txtMemName.Text = "";
            pkrJoinDate.Value =  DateTime.Now;
            currentMem = -1;
            clearSWData();
        }

        private void clearSWData()
        {
            txtSWJobTittle.Text = "";
            txtSWPlace.Text = "";
            txtSWSupervisor.Text = "";
            currentSW = -1;
        }

        private void refreshSWGrid()
        {
            gridSWs.DataSource = SocialWork.GetSocialWorkByMemId(currentMem);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            clearAllFormData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED); 
            else
            {
                EvaluationEntity oldEnttiy = Evaluation.GetEvaluationByE_ID(currentMem);
                EvaluationEntity newEntity = fillEvalEntityFromForm();
                if (newEntity == null)
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                else
                {
                    if (oldEnttiy != null)
                    {
                        newEntity.e_Id = oldEnttiy.e_Id;
                        flag &= Evaluation.Update(newEntity);
                    }
                    else
                        flag &= Evaluation.Add(newEntity);

                    if (flag)
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }
        }


        private void button9_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else if (showConfirmDeleteMessage())
            {
                if (Evaluation.Delete(currentMem))
                {
                    clearAllFormData();
                    showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            clearSWData();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
            else
            {
                SocialWorkEntity entity = fillSWEntity();
                bool flag = true;
                if (entity == null)
                {
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                }
                else
                {
                    if (entity.Id == -1)
                        flag &= SocialWork.Add(entity);
                    else
                        flag &= SocialWork.Update(entity);

                    if (flag)
                    {
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                        clearSWData();
                        refreshSWGrid();
                    }
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }
        }

        private void txtMemName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                List<MemberBasicInfoEntity> members = null;
                if (currentUser.UType == User.SUPERVISOT_USER_UType)
                    members = MemberBasicInfo.GetAllMembersFilteringByGroup(currentUser.Group);
                else if (currentUser.UType == User.ADMIN_USER_UType)
                    members = MemberBasicInfo.GetAllMembers();

                if (members == null)
                    MessageBox.Show("لاتملك صلاحية لمشاهدة هذه البيانات");
                else if (members.Count == 0)
                {
                    MessageBox.Show("لا يوجد بيانات ");
                }
                else
                {
                    gridMembers.DataSource = members;
                    gridMembers.Visible = true;
                }
            }
        }

        private void gridMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                MemberBasicInfoEntity memberEntity = ((List<MemberBasicInfoEntity>)gridMembers.DataSource)[e.RowIndex];
                currentMem = memberEntity.m_Id;
                txtMemName.Text = memberEntity.m_Mem_Name;
                lblGroupName.Text = GroupsNames.GetGroupByG_ID(currentUser.Group).G_Name;
                lblSupervisor.Text = currentUser.UName;

                EvaluationEntity evalEntity = Evaluation.GetEvaluationByE_ID(memberEntity.m_Id);
                if (evalEntity != null)
                    publishEvalEntityToForm(evalEntity);

                refreshSWGrid();
                gridMembers.Visible = false;
            }
        }

        private void gridSWs_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void gridSWs_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                publishSWEntityToForm(((List<SocialWorkEntity>)gridSWs.DataSource)[e.RowIndex]);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (currentMem == -1 || currentSW == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else if (showConfirmDeleteMessage())
            {
                if (SocialWork.Delete(currentSW))
                {
                    clearSWData();
                    refreshSWGrid();
                    showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                }
            }
        }
    }
}
